package net.gz01.wallet.web.controller.admin;

import java.io.File;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

import com.jfinal.upload.UploadFile;
import net.gz01.util.ZODate;
import net.gz01.util.ZOLog;
import net.gz01.wallet.common.PageRespon;
import net.gz01.wallet.common.ZOSearchCondition;
import net.gz01.wallet.web.config.SystemConfig;
import net.gz01.wallet.web.controller.BaseController;
import net.gz01.wallet.web.model.AdminUser;
import net.gz01.wallet.web.model.HelpInfo;

public class HelpController extends BaseController {

	private static final String GconfigJson = "{\n" +
			"  /* 上传图片配置项 */\n" +
			"  \"imageActionName\": \"uploadimage\", /* 执行上传图片的action名称 */\n" +
			"  \"imageFieldName\": \"upfile\", /* 提交的图片表单名称 */\n" +
			"  \"imageMaxSize\": 2048000, /* 上传大小限制，单位B */\n" +
			"  \"imageAllowFiles\": [\".png\", \".jpg\", \".jpeg\", \".gif\"], /* 上传图片格式显示 */\n" +
			"  \"imageCompressEnable\": true, /* 是否压缩图片,默认是true */\n" +
			"  \"imageCompressBorder\": 1600, /* 图片压缩最长边限制 */\n" +
			"  \"imageInsertAlign\": \"none\", /* 插入的图片浮动方式 */\n" +
			"  \"imageUrlPrefix\": \"http://127.0.0.1:8033/\", /* 图片访问路径前缀 */\n" +
			"  \"imagePathFormat\": \"/upload/{yyyy}{mm}{dd}/{hh}{ii}{ss}\"\n" +
			"}";

	public void search() {
		List<HelpInfo> list = HelpInfo.dao.selectHelpType();
		setAttr("helptype",list);
		render("help/helpList.html");
	}

	public void listJson() {
		ZOSearchCondition sc = super.getSearchCondition();
		Page<HelpInfo> pageResult = HelpInfo.dao.search(sc);
		renderJson(new PageRespon(pageResult));
	}

	public void view() {
		HelpInfo hi = HelpInfo.dao.findById(getPara("id"));
		setAttr("helpinfo", hi);
		render("help/helpView.html");
	}

	public void addPage(){
		List<HelpInfo> list = HelpInfo.dao.selectHelpType();
		setAttr("helptype",list);
		render("help/helpAdd.html");
	}

	public void add(){
		AdminUser user = (AdminUser)getSession().getAttribute("loginAdminUser");
		HelpInfo helpInfo = getModel(HelpInfo.class);
		helpInfo.setContent(getPara("content"));
		helpInfo.setCreatetime(ZODate.getDateTime());
		helpInfo.setUpdatetime(ZODate.getDateTime());
		helpInfo.setEditor(user.getName());
		helpInfo.save();
		renderSuccess();

	}

	public void update(){
		HelpInfo helpInfo = getModel(HelpInfo.class);
		helpInfo.setContent(getPara("content"));
		helpInfo.setUpdatetime(ZODate.getDateTime());
		AdminUser user = (AdminUser)getSession().getAttribute("loginAdminUser");
		helpInfo.setEditor(user.getName());
		helpInfo.update();
		renderSuccess();
	}

	public void updatePage(){
		HelpInfo helpInfo = HelpInfo.dao.findById(getPara("id"));
		List<HelpInfo> list = HelpInfo.dao.selectHelpType();
		setAttr("helptype",list);
		setAttr("helpinfo", helpInfo);
		render("help/helpUpdate.html");
	}

	@Before(Tx.class)
	public void delete(){
		HelpInfo.dao.deleteById(getPara("id"));
		renderSuccess(false);
	}



	public void uploadFile(){
		if("config".equals(getPara("action"))){
			String configJson = GconfigJson.replaceAll("/\\*(.|[\\r\\n])*?\\*/","");
			JSONObject json = JSON.parseObject(configJson);
			json.put("imageUrlPrefix", SystemConfig.config.get("local_domain"));
			renderJson(json.toString());
			return;
		}

		List<UploadFile> uploadFileList =  getFiles();
		if(uploadFileList!=null && !uploadFileList.isEmpty()){
			ZOLog.api.info("有文件信息!");
			File file = uploadFileList.get(0).getFile();

			String jsonStr = "{\n" +
					"\"state\": \"SUCCESS\",\n" +
					"\"original\": \"80px - \\u526f\\u672c (2).jpg\",\n" +
					"\"size\": \"13252\",\n" +
					"\"title\": \"1465731377326075274.jpg\",\n" +
					"\"type\": \".jpg\",\n" +
					"\"url\": \"/ueditor/jsp/upload/image/20160612/1465731377326075274.jpg\"\n" +
					"}";

			jsonStr = jsonStr.replaceAll("/\\*(.|[\\r\\n])*?\\*/","");
			JSONObject json = JSON.parseObject(jsonStr);
			json.put("title", file.getName());
			json.put("size", file.length());
			json.put("url", "/upload/" + file.getName());
			json.put("original", file.getName());
			renderText(json.toJSONString());
		}


	}

}
